package com.android.ailaoshan.index;

import android.os.Bundle;
import android.widget.Toast;

import com.android.ailaoshan.R;
import com.android.ailaoshan.common.BaseApplication;
import com.android.ailaoshan.index.fragment.CollectionFragment;
import com.android.ailaoshan.index.fragment.MapFragment;
import com.android.ailaoshan.index.fragment.MineFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

public class MainActivity extends AppCompatActivity {

    private BottomNavigationView bottomBar;
    /**
     * 碎片标识
     * 0 地图碎片
     * 1 收集页
     * 2 声音碎片
     */
    private int fragmentId = 0;

    private long pressTime;

    private BaseApplication baseApplication;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_activity);

        bottomBar = findViewById(R.id.bottom_navigation_bar);

        baseApplication = BaseApplication.getInstance();
        baseApplication.addActivity(this);
    }

    @Override
    protected void onResume(){
        super.onResume();

        listenButtonClick();

        dynamicChangeFragment(new MapFragment());
    }

    /**
     * 监听按钮点击
     */
    private void listenButtonClick(){
        bottomBar.setOnNavigationItemSelectedListener(
            (menuItem) -> {
                int itemId = menuItem.getItemId();
                switch (itemId){
                    case R.id.bottom_item_map:{
                        if(fragmentId != 0){
                            dynamicChangeFragment(new MapFragment());
                            fragmentId = 0;
                        }
                        return true;
                    }
                    case R.id.bottom_item_collection:{
                        if(fragmentId != 1){
                            dynamicChangeFragment(new CollectionFragment());
                            fragmentId = 1;
                        }
                        return true;
                    }
                    case R.id.bottom_item_mine:{
                        if(fragmentId != 2){
                            dynamicChangeFragment(new MineFragment());
                            fragmentId = 2;
                        }
                        return true;
                    }
                }
                return false;
            }
        );
    }


    /**
     * 替换碎片
     */
    private void dynamicChangeFragment(Fragment fragment){
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.replace(R.id.main_content_container,fragment);
        transaction.commit();
    }


    /**
     * 退出应用方法
     */
    @Override
    public void onBackPressed() {
        if((System.currentTimeMillis() - pressTime) > 2000){
            Toast.makeText(this,"再按一次退出应用",Toast.LENGTH_SHORT).show();
            this.pressTime = System.currentTimeMillis();
        }else{
            baseApplication.removeAllActivity();
        }
    }
}
